package com.why.getwayserver.filter;

import com.why.pojo.EmployeesAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.HashMap;


public class AuthGatewayFilter implements GatewayFilter, Ordered {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String url = exchange.getRequest().getHeaders().getFirst("url");
        System.out.println(url);

        String token =exchange.getRequest().getHeaders().getFirst("token");
        HashMap<String, Object> map = (HashMap<String, Object>)redisTemplate.opsForValue().get(token);
        EmployeesAccount employeesAccount =(EmployeesAccount) map.get("user");
        System.out.println(employeesAccount.getUserName());
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
